﻿Imports System.Data.OleDb

Public Class ShowIt
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Session("username") = User.Identity.Name

        Dim sql As String = "select  liker from usersheetliking  where liker=@f1 and  sheetid=@f2 "
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("SheetsConnectionString").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)

        cmd.Parameters.AddWithValue("@f2", Request.QueryString("id").ToString)

        Dim dr = cmd.ExecuteReader()

        dr.Read()

        If dr.HasRows Then

            LinkButton1.Enabled = False


        End If
        Catch ex As Exception

        End Try





    End Sub

    Protected Sub btn_AddComnt_Click(sender As Object, e As EventArgs) Handles btn_AddComnt.Click

        Dim sql As String = "INSERT INTO Comments (Author,Title,CommentBody,Sheet_fkID) VALUES (@f1,@f2,@f3,@f4)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("SheetsConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_TitleCom.Text)
        cmd.Parameters.AddWithValue("@f3", tb_ComntBody.Text)
        cmd.Parameters.AddWithValue("@f4", Request.QueryString("ID"))

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("ShowIt.aspx?ID=" & Request.QueryString("ID"))
    End Sub

    Protected Sub LinkButton1_Click(sender As Object, e As EventArgs) Handles LinkButton1.Click
        Try
            Session("username") = User.Identity.Name

            Dim sql As String = "select  liker from usersheetliking  where liker=@f1 and  sheetid=@f2 "
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("SheetsConnectionString").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)

            cmd.Parameters.AddWithValue("@f2", Request.QueryString("id").ToString)

            Dim dr = cmd.ExecuteReader()

            dr.Read()

            If dr.HasRows Then

                Exit Sub

            End If
           

            likedatasource.Insert()
            likedatasource.Update()
            LinkButton1.Enabled = False

        Catch ex As Exception

        End Try



    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles btn_SaveRating.Click

        Dim oleDbConn = New OleDbConnection(ConfigurationManager.ConnectionStrings("SheetsConnectionString").ConnectionString)

        Dim delPrevRatings = "DELETE FROM UserSheetRating WHERE Rater=@f1 AND SheetID=@f2"
        Dim oleDbCmd4 = New OleDbCommand(delPrevRatings, oleDbConn)
        oleDbCmd4.Parameters.AddWithValue("@f1", User.Identity.Name)
        oleDbCmd4.Parameters.AddWithValue("@f2", Request.Params("ID"))
        oleDbCmd4.ExecuteNonQuery()
        oleDbCmd4.Dispose()
        oleDbConn.Dispose()


        Dim sql = "INSERT INTO UserSheetRating (Rating,Rater,SheetID) VALUES (@f1,@f2,@f3)"
        Dim oelDbCmd3 = New OleDbCommand(sql, oleDbConn)
        oleDbConn.Open()
        oelDbCmd3.Parameters.AddWithValue("@f1", Rating1.CurrentRating)
        oelDbCmd3.Parameters.AddWithValue("@f2", User.Identity.Name)
        oelDbCmd3.Parameters.AddWithValue("@f3", Request.Params("ID"))
        oelDbCmd3.ExecuteNonQuery()
        oelDbCmd3.Dispose()
        oleDbConn.Dispose()

    End Sub

    Protected Sub likedatasource_Selecting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles likedatasource.Selecting

    End Sub
End Class